/**
 * Created by Administrator on 2017/5/14 0014.
 */
layui.use('element', function () {
    var element = layui.element();

    //…
});

layui.use('form', function () {
    var $ = layui.jquery, form = layui.form();

    form.on('submit(addsubmit)', function (data) {
        console.log(data.field);
        var params = $.param({
            propagandaname: data.field.propagandaname,
            type: data.field.type,
            url: data.field.propagandaurl,
            photo: data.field.propagandaphoto
        }, true);
        console.log(params);


        $.ajax({
            url: '/addpropaganda.action',
            type: 'post',
            data: params,
            dataType: 'json',
            success: function (data) {
                console.log(data);
                window.location.href = "/admin/propaganda/showdetail";
                layer.msg("添加成功");
            }
        });
    });


    form.on('submit(updatepropaganda)', function (data) {
        console.log(data.field);
        var params = $.param({
            id: data.field.id,
            propagandaname: data.field.propagandaname,
            type: data.field.croPropagandaVostype,
            url: data.field.url,
            photo: data.field.photo
        }, true);
        console.log(params);


        $.ajax({
            url: '/updatepropaganda.action',
            type: 'post',
            data: params,
            dataType: 'json',
            success: function (data) {
                parent.window.location.reload();
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
                layer.msg("修改成功");
            }
        });
    });

});


layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    laypage({
        cont: 'fullpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                var jsonObject = $('#jsonObject').val();
                if (jsonObject.length > 0) {
                    console.log(jsonObject);
                    var objs = eval('(' + jsonObject + ')');//将json转成对象
                    var condition = 'pageIndex=' + pageIndex + '&';
                    condition += "croPropaganda.name=" + objs.propagandaname + '&' + "croPropaganda.type=" + objs.type;

                    console.log(condition);
                    window.location.href = "/admin/propaganda/querylist?" + condition;
                } else {
                    window.location.href = "/admin/propaganda/showdetail?pageIndex=" + pageIndex;
                }
            }
        }
    });
});

layui.use(['laypage', 'layer'], function () {
    var laypage = layui.laypage, layer = layui.layer;
    //地址分页
    laypage({
        cont: 'detailpage',
        pages: $('#pageCount').val(), //分页总数
        skip: true,
        curr: $('#pageIndex').val(),
        jump: function (obj, first) {
            //得到了当前页，用于向服务端请求对应数据
            var cusid = $('#croCustomerid').val();
            if (first != true) {//是否首次进入页面
                var pageIndex = obj.curr;//获取点击的页码
                window.location.href = "/admin/propaganda/?pageIndex=" + pageIndex + '&customerid=' + cusid;
            }
        }
    });
});

layui.use('layer', function () {
    var $ = layui.jquery, layer = layui.layer; //独立版的layer无需执行这一句
    //触发事件
    var active = {
        updatepropaganda: function (othis) {
            //弹出即全屏
            var index = layer.open({
                type: 2,
                title: "修改宣传信息",
                shadeClose: true,
                content: 'listPropaganda?id=' + othis.propagandaid,
                area: ['100%', '100%'],
                maxmin: false
            });
            layer.full(index);
        },


        delpropaganda: function (othis) {
            //删除
            layer.open({
                type: 1,
                title: false, //不显示标题栏
                closeBtn: false,
                area: '300px;',
                shade: 0.8,
                id: 'LAY_layuipro', //设定一个id，防止重复弹出
                btn: ['绝不后悔', '我怂了'],
                moveType: 1, //拖拽模式，0或者1,
                content: '<div style="padding: 30px; line-height: 22px; background-color: #393D49; color: #fff; font-weight: 300;">删除后将不可恢复，是否确认删除<br></div>',
                yes: function (index, layero) {
                    $.ajax({
                        url: "/delpropaganda.action",
                        type: 'post',
                        data: {
                            id: othis.propagandaid,
                            del_flag: "1"
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == "删除成功") {
                                window.location.href = "/admin/propaganda/showdetail";
                                layer.msg("删除成功");
                            } else {
                                layer.msg("删除失败");
                            }
                        }
                    });

                }
            });
        },
    };


    $('.delsumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.propagandaid = $(this).attr('propagandaid');
        active[method] ? active[method].call(this, othis) : '';
    });
    $('.updatesumit').on('click', function () {
        var othis = $(this), method = othis.data('method');
        othis.propagandaid = $(this).attr('propagandaid');
        active[method] ? active[method].call(this, othis) : '';
    });
});

